package com.leased.service.impl;

import com.leased.exception.DatabaseException;
import com.leased.interceptor.PageMybatisInterceptor;
import com.leased.mapper.AdvertInfoMapper;
import com.leased.pojo.AdvertInfo;
import com.leased.pojo.AdvertInfoExample;
import com.leased.request.BaseQuery;
import com.leased.service.AdvertInfoService;
import com.leased.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by shencx on 2017/8/9.
 */
@Service
public class AdvertInfoServiceImpl implements AdvertInfoService{

    Logger LOG = LoggerFactory.getLogger(AdminInfoServiceImpl.class);

    @Resource
    private AdvertInfoMapper advertInfoMapper;

    /**
     * 添加广告
     * @param advertInfo
     * @return
     * @throws DatabaseException
     */
    @Override
    public Integer addAdvertInfo(AdvertInfo advertInfo) throws DatabaseException {
        try {
            if (advertInfo == null){
                LOG.error("addAdvertInfo 信息为空",advertInfo);
                return Integer.valueOf(0);
            }
            advertInfo.setCreateTime(new Date());
            advertInfo.setBeUsed(1);
            int val = advertInfoMapper.insertSelective(advertInfo);
            if (val > 0){
                return advertInfo.getId();
            }else {
                return Integer.valueOf(0);
            }
        } catch (Throwable e) {
            LOG.error("addAdvertInfo 发生异常",e,advertInfo);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取广告信息列表
     * @param baseQuery
     * @return
     * @throws DatabaseException
     */
    @Override
    public Page<AdvertInfo> fetchAdvertInfoPage(BaseQuery baseQuery) throws DatabaseException {
        try {
            if (baseQuery == null){
                LOG.error("fetchAdvertInfoPage 信息为空",baseQuery);
                return null;
            }
            AdvertInfoExample example = new AdvertInfoExample();
            example.setOrderByClause("create_time desc");
            PageMybatisInterceptor.startPage(baseQuery.getPageNum(),baseQuery.getPageSize());
            advertInfoMapper.selectByExample(example);
            Page<AdvertInfo> page = PageMybatisInterceptor.endPage();
            return page;
        } catch (Throwable e) {
            LOG.error("fetchAdvertInfoPage 发生异常",e,baseQuery);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 更新广告信息
     * @param advertInfo
     * @return
     * @throws DatabaseException
     */
    @Override
    public boolean updateAdvertInfo(AdvertInfo advertInfo) throws DatabaseException {
        try {
            if (advertInfo == null){
                LOG.error("updateAdvertInfo 信息为空",advertInfo);
                return false;
            }
            Integer val = advertInfoMapper.updateByPrimaryKeySelective(advertInfo);
            return val>0?true:false;
        } catch (Throwable e) {
            LOG.error("updateAdvertInfo 发生异常",e,advertInfo);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取广告详情
     * @param id
     * @return
     * @throws DatabaseException
     */
    @Override
    public AdvertInfo fetchAdvertInfoDetail(Integer id) throws DatabaseException {
        try {
            if (id == null){
                LOG.error("fetchAdvertInfoDetail id 为空",id);
                return null;
            }
            AdvertInfo advertInfo = advertInfoMapper.selectByPrimaryKey(id);
            return advertInfo;
        } catch (Throwable e) {
            LOG.error("fetchAdvertInfoDetail 发生异常",e,id);
            throw new DatabaseException(e.getMessage());
        }
    }
}
